package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.model.WwdxWeixinUserModel;
import com.wwdx.service.WwdxWeixinUserServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 */
@Service
public class WwdxWeixinUserServiceImpl extends BaseServiceImpl<WwdxWeixinUserModel> implements WwdxWeixinUserServiceI {

    @Override
    public Long selectModelCount(WwdxWeixinUserModel model){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
        String hql = "select count(1) from WwdxWeixinUserModel\t"+getParams(model,params);
        return count(hql,params);
    }

    private String getParams(WwdxWeixinUserModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\n");
        String appid = model.getAppid();
        if (StringUtils.isNotBlank(appid)){
            params.put("appid",appid);
            sb.append("and appid=:appid\t");
        }
        return sb.toString();
    }

    @Override
    public List<WwdxWeixinUserModel> selectModelList(WwdxWeixinUserModel model, Integer page, Integer rows){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
        String hql = "from WwdxWeixinUserModel\t"+getParams(model,params);
        return find(hql,params,page,rows);
    }

    @Override
    public WwdxWeixinUserModel getByOpenId(String openid) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("openid",openid);
        return getByHql("from WwdxWeixinUserModel where openid=:openid", params);
    }

    @Override
    public WwdxWeixinUserModel getByUnionId(String appid, String unionid) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("appid",appid);
        params.put("unionid",unionid);
        return getByHql("from WwdxWeixinUserModel where appid=:appid and unionid=:unionid", params);
    }
}